import datetime
import time
import mySqlFunc
from config import *
import schedule


def sessionRemover():
    sqlConn = mySqlFunc.MysqlConnector(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE)
    res = sqlConn.Select(f"select session_id, expire_time from {SESSION_TABLE}")
    for s in res:
        session_id, expire_time = s
        if expire_time < datetime.datetime.now():
            sqlConn.Execute(f"delete from {SESSION_TABLE} where session_id='{session_id}'")


def sessionChecker():
    # 每分钟删除一次过期的session
    schedule.every(1).minute.do(sessionRemover)
    while True:
        schedule.run_pending()
        time.sleep(1)
